from pyecharts.charts import Bar


def analysis():
    with open("history.txt", "r", encoding='utf-8') as fp:
        lines = fp.readlines()
        llen = len(lines)
        r1, r2, r3, r4, r5, b1, b2 = 0, 0, 0, 0, 0, 0, 0
        rmap = {}
        bmap = {}
        for line in lines:
            # print(line)
            strs = line.split(' ')
            if strs[2] in rmap.keys():
                rmap[strs[2]] = rmap[strs[2]] + 1
            else:
                rmap[strs[2]] = 1

            if strs[3] in rmap.keys():
                rmap[strs[3]] = rmap[strs[3]] + 1
            else:
                rmap[strs[3]] = 1

            if strs[4] in rmap.keys():
                rmap[strs[4]] = rmap[strs[4]] + 1
            else:
                rmap[strs[4]] = 1

            if strs[5] in rmap.keys():
                rmap[strs[5]] = rmap[strs[5]] + 1
            else:
                rmap[strs[5]] = 1

            if strs[6] in rmap.keys():
                rmap[strs[6]] = rmap[strs[6]] + 1
            else:
                rmap[strs[6]] = 1

            if strs[7] in bmap.keys():
                bmap[strs[7]] = bmap[strs[7]] + 1
            else:
                bmap[strs[7]] = 1

            if strs[8] in bmap.keys():
                bmap[strs[8]] = bmap[strs[8]] + 1
            else:
                bmap[strs[8]] = 1

            r1 += int(strs[2])
            r2 += int(strs[3])
            r3 += int(strs[4])
            r4 += int(strs[5])
            r5 += int(strs[6])
            b1 += int(strs[7])
            b2 += int(strs[8])
        print(r1, r2, r3, r4, r5, b1, b2)
        print(r1 / llen, r2 / llen, r3 / llen, r4 /
              llen, r5 / llen, b1 / llen, b2 / llen)

        rmap = sorted(rmap.items(), key=lambda x: x[0])
        bmap = sorted(bmap.items(), key=lambda x: x[0])
        print(rmap, bmap)
        rx = []
        ry = []
        bx = []
        by = []
        for key in rmap:
            rx.append(key[0])
            ry.append(key[1])
        for key in bmap:
            bx.append(key[0])
            by.append(key[1])
        print(rx, ry, bx, by)

        bar = Bar()
        bar.add_xaxis(rx)
        bar.add_yaxis('RED', ry)
        bar.add_yaxis('BLUE', by)
        # bar.set_global_opts(title_opts=opts.TitleOpts(title="---"))
        bar.render()


if __name__ == "__main__":
    analysis()
